package br.com.gbvbahia.mybooks.fragment;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;
import br.com.gbvbahia.commons.Transacao.Transacao;
import br.com.gbvbahia.mybooks.ActivityMae;
import br.com.gbvbahia.mybooks.R;
import br.com.gbvbahia.mybooks.adapters.LivroPesquisaResultAdapter;
import br.com.gbvbahia.mybooks.exceptions.NoConectionException;
import br.com.gbvbahia.mybooks.modelos.LivroVO;
import br.com.gbvbahia.mybooks.service.ws.WebService;

public class ListarLivrosPesquisaFragment extends FragmentMae implements
		Transacao {
	private ListView listView;
	private List<LivroVO> livros;
	private String filtroPesquisa;
	private EditText searchEdit;
	private ImageButton searchButton;
	private WebService service;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.listar_pesquisar, null);
		listView = (ListView) view.findViewById(R.id.listview);
		searchEdit = (EditText) view.findViewById(R.id.searchTextHeader);
		searchButton = (ImageButton) view.findViewById(R.id.btnSearch);
		service = new WebService(getActivity());
		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int posicao, long id) {
				LivroVO c = (LivroVO) parent.getAdapter().getItem(posicao);
				FragmentTransfer ft = (FragmentTransfer) getActivity();
				ft.updateFragment(R.layout.listar_pesquisar, c);
			}
		});

		searchButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				pesquisa();
			}
		});

		return view;
	}

	@Override
	public void executar() throws Exception {
		try {
			livros = service.pesquisarAvaliacoes(filtroPesquisa);
		} catch (NoConectionException e) {
			livros = null;
		}
	}

	@Override
	public void atualizarView() {
		if (getActivity() != null && !getActivity().isFinishing()) {
			if (livros == null) {
				Toast.makeText(getActivity(), getString(R.string.aviso_39),
						Toast.LENGTH_SHORT).show();
				return;
			}
			listView.setAdapter(new LivroPesquisaResultAdapter(this, livros));
			FragmentTransfer ft = (FragmentTransfer) getActivity();
			ft.updateFragment(R.layout.frag_livroscount,
					Integer.valueOf(livros.size()));
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		if (!StringUtils.isBlank(filtroPesquisa)) {
			startTransacao(this);
		}
	}

	private void pesquisa() {
		filtroPesquisa = searchEdit.getText().toString();
		if (TextUtils.isEmpty(filtroPesquisa)) {
			Toast.makeText(getActivity(), getString(R.string.aviso_36),
					Toast.LENGTH_SHORT).show();
			return;
		}
		Log.d(ActivityMae.LOG_TAG, "BUSCABDO WEBSERVICE LIVRO OnKey: "
				+ filtroPesquisa);
		startTransacao(ListarLivrosPesquisaFragment.this);
	}
}
